package com.prognos.persistence.dao.impl;

import com.prognos.domain.info.InfoSource;
import com.prognos.persistence.AbstractDaoSupport;
import com.prognos.persistence.dao.InfoSourceDao;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository("InfoSourceDao")
@Transactional
public class InfoSourceDaoImpl extends AbstractDaoSupport<InfoSource, Long> implements InfoSourceDao {

    @Autowired
     public InfoSourceDaoImpl(SessionFactory sessionFactory) {
        super(InfoSource.class, sessionFactory);
    }

    @Override
    public InfoSource findByUrl(String url) {
        Criteria criteria = getDefaultCriteria();
        criteria.add(Restrictions.eq("url", url));
        List infoSources = criteria.list();
        return CollectionUtils.isEmpty(infoSources)
                ? null
                : (InfoSource) infoSources.get(0);

    }
}
